package sk.naive.talker.command;

import sk.naive.talker.*;
import sk.naive.talker.util.Utils;
import sk.naive.talker.props.*;
import sk.naive.talker.message.DefaultMessageFactory;

import java.rmi.RemoteException;
import java.util.*;

/**
 * @author <a href="mailto:rytier@naive.deepblue.sk">Martin "Rytier" Kerni</a>
 * @version $Revision: 1.25 $ $Date: 2005/03/15 19:59:58 $
 */
public class Rooms extends AbstractCommand {
	public void exec()
		throws CommandException,
		RemoteException
	{
		LocationFinder lf = commandDispatcher.locationFinder();
		if (lf.locations().size() == 0) {
			user.send(getString("rooms.notDefined", user.getProperties(), user.getLang()));
			return;
		} else {
			boolean isSuperuser = Utils.ownsPermission(user, Consts.PERM_SUPERUSER);
			boolean isRoomAdmin = Utils.ownsPermission(user, RoomAdmin.PERM_ROOM_ADMIN);
			Map locEntrancesCount = new HashMap();
			for (Iterator i = lf.locations().iterator(); i.hasNext();) {
				Location loc = (Location) i.next();
				Integer count = (Integer) locEntrancesCount.get(loc);
				if (count == null) {
					locEntrancesCount.put(loc, new Integer(0));
				}
				for (Iterator outs = loc.nearLocations().iterator(); outs.hasNext();) {
					Location neighbour = (Location) outs.next();
					count = (Integer) locEntrancesCount.get(neighbour);
					if (count == null) {
						locEntrancesCount.put(neighbour, new Integer(1));
					} else {
						locEntrancesCount.put(neighbour, new Integer(count.intValue() + 1));
					}
				}
			}
			StringBuilder sb = new StringBuilder();
			sb.append(getString("rooms.head", user.getProperties(), user.getLang()));
			Set locs = new TreeSet(new Comparator() {
				public int compare(Object o1, Object o2) {
					return ((Location) o1).getName().compareTo(((Location) o2).getName());
				}
			});
			locs.addAll(lf.locations());
			for (Iterator i = locs.iterator(); i.hasNext();) {
				Location loc = (Location) i.next();
				if (isSuperuser || isRoomAdmin || BooleanProperty.booleanValue((String) loc.get(Location.LPROP_VISIBILITY)) || loc.getLocationOwners().contains(user.getId())) {
					user.getProperties().put(DefaultMessageFactory.CTXKEY_VAL, loc.getName());
					user.getProperties().put(DefaultMessageFactory.CTXKEY_VAL + 1, locEntrancesCount.get(loc));
					user.getProperties().put(DefaultMessageFactory.CTXKEY_VAL + 2, loc.nearLocations().size());
					user.getProperties().put(DefaultMessageFactory.CTXKEY_VAL + 3, loc.users().size());
					String topic = loc.getTopic();
					if (topic != null) {
						user.getProperties().put(DefaultMessageFactory.CTXKEY_VAL + 4, topic);
						User topicAuthor = commandDispatcher.getUserFinder().findUserById(loc.getTopicAuthorId());
						if (topicAuthor != null) {
							user.getProperties().put(DefaultMessageFactory.CTXKEY_VAL + "TopicAuthor", topicAuthor.getProperties());
							sb.append(getString("rooms.line", user.getProperties()));
						} else {
							sb.append(getString("rooms.noAuthorLine", user.getProperties()));
						}
					} else {
						sb.append(getString("rooms.noTopicLine", user.getProperties()));
					}
				}
			}
			sb.append(getString("rooms.tail", user.getProperties(), user.getLang()));
			user.send(sb.toString());

		}
	}
}

